﻿(function ($) {
    var isOpening = false;
    $(".book-share-button").each(function () {
        var button = $(this);
        var panel = button.find(".services");


        button.on("click", function () {
            var newState = panel.css('display') == 'none' ? 'block' : 'none';

            if (newState == 'block') {
                // close all others
                $(".book-share-button .services").each(function () {
                    var $this = $(this);
                    if ($this.css('display') == 'block') {
                        $this.fadeOut("slow");
                    }
                });
                isOpening = true;
                panel.fadeIn('slow');
            }
        });
    });

    $(document).click(function () {
        if (isOpening) {
            isOpening = false;
            return;
        }
        
        var panels = $(".book-share-button .services");
        panels.each(function () {
            var panel = $(this);
            if (!isOpening && panel.css('display') != 'none') {
                panel.fadeOut('slow');
            }
        });
    });
})(jQuery);